// packageA/pages/employee/employee.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    isScorll: false,
    pageSize: 10,
    pageNum: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  editClick(e){
    wx.navigateTo({
      url: '/packageA/pages/addEmployee/addEmployee?id=' + e.target.dataset.id,
    })
  },
  deletClick(e){
    console.log(e);
    let that = this
    db.collection('employeeInfo').doc(e.target.dataset.id).remove({
      success: function(res) {
        that.setData({
          pageNum: 0,
          list: [],
        },()=>{
          that.getList()
        })
      }
    })
  },
  moreClick(e){
    console.log(e);
    const storeId = e.target.dataset.storeId; 
    for (const item of this.data.list) {
        if (item._id === storeId) continue;
        this.selectComponent("#vanSwipeCell"+item._id).close();
    } 
    wx.nextTick(()=>{this.selectComponent("#vanSwipeCell" + storeId).open({position: "right"})})
  },
  getList(){
    this.setData({isScorll: false})
    let that = this
    db.collection('employeeInfo').orderBy('date', 'desc').limit(that.data.pageSize).skip(that.data.pageNum).get().then(res=>{
      console.log(res);
      let oldData = that.data.list
      let newData = oldData.concat(res.data)
      if(res.data.length != 0){
        that.setData({
          isScorll: true
        })
      }
      that.setData({
        list: newData
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({list: []},()=>{this.getList()})
  },
  refresh(){
    this.setData({
      pageNum: 0,
      list: [],
    },()=>{
      this.getList()
    })
  },
  addemployee(){
    wx.navigateTo({
      url: '/packageA/pages/addEmployee/addEmployee',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },
  bindscrolltolower(){
    if(this.data.isScorll){
      let pageNum = this.data.pageNum + this.data.pageSize
      this.setData({
        pageNum: pageNum,
        pageSize: this.data.pageSize
      }, ()=>{this.getList()})
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})